#pragma once

#include <stdarg.h>
#include <iostream>
#include <time.h>

#define SIZE 1024

#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

std::string levelToString(int level)
{
    switch(level)
    {
        case Info : return "Info";
        case Debug : return "Debug";
        case Warning : return "Warning";
        case Error : return "Error";
        case Fatal : return "Fatal";
        default : return "None";
    }
}

void logmessage(int level, char* format, ...)
{
    char leftbuffer[SIZE];
    time_t t = time(nullptr);
    struct tm* ctime = localtime(&t);
    snprintf(leftbuffer, SIZE,
        "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(), 
        ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
    char rightbuffer[SIZE];
    va_list s;
    va_start(s, format);
    vsnprintf(rightbuffer, SIZE, format, s);
    va_end(s);
    
    char logtxt[2*SIZE];
    snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);
    std::cout << logtxt << std::endl;
}   

// int sum(int n, ...)
// {
//     //char* 可以去提取一个一个的参数
//     va_list s;
//     va_start(s, n);
//     int SUM = 0;
//     for(int i = 0; i < n; i++)
//     {
//         SUM += va_arg(s,int);
//     }
//     va_end(s); //s = NULL
//     return SUM;
// }
